var express = require('express');
var router = express.Router();
const path=require('path');
const fs=require('fs');

// /* GET home page. */
router.get('/login', function(req, res) {
  res.render('login');
});


router.post('/login',(req,res,next)=>{
  let {username,password}=req.body;
console.log(username,password);

  // 判断输入是否为空
  if(!username||!password){
    next('请输入有效内容');
    return;
  }

  // 获取数据
  let data=JSON.parse(fs.readFileSync(path.join(__dirname,'../persons.json')))
// 判断是否正确
let s=data.find(item=>username==item.username&&password==item.password);
if(s){
  res.render('public',{end:'登录成功'});
  return;
}else{
  next('账号或密码有错，请重新输入')
}

})

module.exports = router;
